# _*_ coding:utf-8 _*_
# @Time :2020/11/29 12:28
# @Author :yanxia

from flask import request

from . import quality_control
from ...apps.quality_control.service import bug_calculation
from ...apps.quality_control.service import project_statistics
from ...apps.quality_control.service import qdata_standard
from ...libs import const
from ...libs.framework import *
from ...libs.validate_params import validate_params


@quality_control.route("/query_data")
def query_quality_data():
    """
    查询截止当前系统日期时，上一个月的项目总和、bug密度标准值、引入错误率标准值。
    :return: json
    """
    data = qdata_standard.qulity_data()
    return result(const.SUCCESS, None, data)


@quality_control.route('/procount', methods=['post'])
@validate_params(params=["page_num", "page_per"])
def countweek():
    """
    项目统计,查询最近一周的项目检查点的数据/首轮漏测率、bug密度等
    :return:json
    """
    data = project_statistics.pro_statis(request.json)
    return result(const.SUCCESS, None, data)


@quality_control.route('/bug_calculate', methods=['post'])
def bug_calculate():
    """
    bug率计算
    :return: json
    """
    addtime = time.strftime('%Y-%m-%d', time.localtime(time.time()))
    data = request.json
    vname = data.get('group_name')
    proname = data.get('project_name')
    versionname = data.get('version_numb')
    name = data.get('note')
    checknum = data.get('checkpoint_numb')
    fristnum = data.get('frist_bugs')
    leaknum = data.get('miss_bugs')
    newnum = data.get('new_bugs')
    bugcount = data.get('bug_totals')
    test = [vname, proname, versionname, name, checknum, fristnum, leaknum, newnum, bugcount]
    if all(not e for e in test):
        return result(const.FAIL_40000, const.PARAM_NOT_NULL, None)
    bug_calculation.Bug_Calculate.insert_bug(vname, proname, versionname, name, checknum, fristnum, leaknum, newnum,
                                             bugcount, addtime)
    data = bug_calculation.Bug_Calculate.get_bugdata(vname, proname, versionname)
    return result(const.SUCCESS, None, data)


@quality_control.route('/static_day', methods=['get'])
def calcu_day():
    """
    查询bug率统计（天，按往前推28天计算的）
    :return:
    """
    ret = bug_calculation.Bug_Calculate.bugstatic_day()
    return result(const.SUCCESS, None, ret)


@quality_control.route('/calcu_param', methods=['post'])
@validate_params(params=["version_name", "project_id"])
def calcu_param():
    """
    查询bug率数据（天）
    :return:
    """
    ret = bug_calculation.Bug_Calculate.calcu_param(request.json)
    return result(const.SUCCESS, None, ret)

